package util;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

import ds.BugInfo;

public class MapUtil {
	/**
	 * get bugID to bugLabel map
	 */
	public static HashMap<String, String> getGroundTruth(ArrayList<BugInfo> allBugs){
		HashMap<String, String> bugId2Class = new HashMap<String, String>();
		for(BugInfo bug : allBugs){
			String bugId = bug.bugID;
			String className = bug.label;
			if(!bugId2Class.containsKey(bugId)){
				bugId2Class.put(bugId, className);
			}
		}
		return bugId2Class;
	}
	
	/**
	 * get BugId of unlabeled bugs
	 * @param allBugs
	 * @return
	 */
	public static HashSet<String> getUnlabeledBugId(ArrayList<BugInfo> allBugs){
		HashSet<String> unlabeledIdSet = new HashSet<String>();
		
		for(BugInfo bug : allBugs){
			String bugId = bug.bugID;
			String className = bug.label;
			if(className.equals(""))
				unlabeledIdSet.add(bugId);
		}
		
		return unlabeledIdSet;
	}
	
	public static void savePosId(String fileName, String label, ArrayList<BugInfo> allBugs){
		try {
			FileWriter fw = new FileWriter(fileName);
			for(BugInfo curBug : allBugs){
				if(curBug.label.equals(label))
					fw.write(curBug.bugID + "\n");
			}
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static HashSet<String> readPosId(String fileName){
		HashSet<String> posID = new HashSet<String>();
		try {
			BufferedReader br = new BufferedReader(new FileReader(fileName));
			for(String curLine = br.readLine(); curLine != null; curLine = br.readLine()){
				posID.add(curLine);
			}
			br.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return posID;
	}
	
}
